package com.zues.java_test.stream;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;

/**
 * @ClassName StreamUtils
 * @Description 自己测试的stream工具类
 * @Author zeus
 * @Date 3/11/2025 1:44 下午
 * @Version 1.0
 **/
public class StreamUtils {

    /**
     * 数据初始化
     * @return 对象集合
     */
    public static List<MyObject> getMyObjects() {
        List<MyObject> objects = Arrays.asList(
                new MyObject(1, "Object1" , "TypeA" , 3),
                new MyObject(2, "Object2" , "TypeB" , 1),
                new MyObject(3, "Object3" , "TypeA" , 2),
                new MyObject(4, "Object4" , "TypeB" , 2),
                new MyObject(5, "Object5" , "TypeA" , 1)
        );
        return objects;
    }

    /**
     * 获取指定id的下一个对象
     * @param objects 对象集合
     * @param id 指定的ID
     * @return 下一个对象
     */
    public static Optional<MyObject> getNextObjectById(List<MyObject> objects, Integer id) {
        return IntStream.range(0, objects.size())
                .filter(i -> objects.get(i).getId().equals(id) && i < objects.size() - 1)
                .mapToObj(i -> objects.get(i + 1))
                .findFirst();
    }

}
